﻿#include "registerhotel.h"
#include "ui_registerhotel.h"
#include <QFileDialog>
#include <QDateTime>
#include <QMessageBox>

RegisterHotel::RegisterHotel(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterHotel)
{
    ui->setupUi(this);

    m_state = E_None;
    ui->de_openedTime->setDisplayFormat("yyyy");
    ui->de_decoratedTime->setDisplayFormat("yyyy");
}

RegisterHotel::~RegisterHotel()
{
    delete ui;
}

void RegisterHotel::setHotelInfo()
{
    forbidEveryOne();
    ui->pb_finish->setText("修改");
    ui->pb_cancel->setText("退出");
    m_hotel = GlobalVars::g_localHotel;
    m_licenseImagePath = "";
    m_imagePathList.clear();
    m_hotelServer = m_hotel.getServer().split(",");

    ui->le_hotelName->setText(m_hotel.getName());
    ui->le_hotelPhone->setText(m_hotel.getPhone());
    ui->cb_hotelStar->setCurrentText(m_hotel.getStar());
    ui->cb_hotelType->setCurrentText(m_hotel.getType());
    ui->le_hotelLocation->setText(m_hotel.getLocation());

    ui->sb_hotelHouseCount->setValue(m_hotel.getTotalHouse());
    ui->cb_lowPrice->setCurrentText(QString::number(m_hotel.getLowPrice()));
    ui->cb_highPrice->setCurrentText(QString::number(m_hotel.getHighPrice()));
    ui->de_openedTime->setDate(QDate::fromString(m_hotel.getOpenedTime(),"yyyy"));
    ui->de_decoratedTime->setDate(QDate::fromString(m_hotel.getDecoratedTime(),"yyyy"));

    ui->te_hotelDescription->setText(m_hotel.getDescription());
    ui->te_hotelIntroduce->setText(m_hotel.getInstruction());
    ui->te_hotelCancel->setText(m_hotel.getCancellation());
    setServerShow(m_hotelServer);

    QStringList cityList = m_hotel.getCity().split("-");
    if (cityList.size() == 3)
    {
        ui->cb_hotelState->setCurrentText(cityList.at(0));
        ui->cb_hotelCity->setCurrentText(cityList.at(1));
        ui->cb_hotelCounty->setCurrentText(cityList.at(2));
    }
}

void RegisterHotel::setServerShow(QStringList serverList)
{
    if (serverList.contains(ui->cb_freePark->text()))
    {
        ui->cb_freePark->setChecked(true);
        serverList.removeOne(ui->cb_freePark->text());
    }
    if (serverList.contains(ui->cb_payPark->text()))
    {
        ui->cb_payPark->setChecked(true);
        serverList.removeOne(ui->cb_payPark->text());
    }
    if (serverList.contains(ui->cb_transfer->text()))
    {
        ui->cb_transfer->setChecked(true);
        serverList.removeOne(ui->cb_transfer->text());
    }
    if (serverList.contains(ui->cb_bath->text()))
    {
        ui->cb_bath->setChecked(true);
        serverList.removeOne(ui->cb_bath->text());
    }
    if (serverList.contains(ui->cb_hairDryer->text()))
    {
        ui->cb_hairDryer->setChecked(true);
        serverList.removeOne(ui->cb_hairDryer->text());
    }
    if (serverList.contains(ui->cb_spa->text()))
    {
        ui->cb_spa->setChecked(true);
        serverList.removeOne(ui->cb_spa->text());
    }
    if (serverList.contains(ui->cb_wiredNet->text()))
    {
        ui->cb_wiredNet->setChecked(true);
        serverList.removeOne(ui->cb_wiredNet->text());
    }
    if (serverList.contains(ui->cb_wifiNet->text()))
    {
        ui->cb_wifiNet->setChecked(true);
        serverList.removeOne(ui->cb_wifiNet->text());
    }
    if (serverList.contains(ui->cb_baggage->text()))
    {
        ui->cb_baggage->setChecked(true);
        serverList.removeOne(ui->cb_baggage->text());
    }
    if (serverList.contains(ui->cb_restaurant->text()))
    {
        ui->cb_restaurant->setChecked(true);
        serverList.removeOne(ui->cb_restaurant->text());
    }
}

void RegisterHotel::setImages()
{
    qDebug() << "image.count = " << GlobalVars::g_hotelImageList.size();

    switch (GlobalVars::g_hotelImageList.size())
    {
        case 0: return;
        default:
        case 7: ui->lb_image6->setPixmap(QPixmap::fromImage(GlobalVars::g_hotelImageList.at(6).scaled(100,100)));
        case 6: ui->lb_image5->setPixmap(QPixmap::fromImage(GlobalVars::g_hotelImageList.at(5).scaled(100,100)));
        case 5: ui->lb_image4->setPixmap(QPixmap::fromImage(GlobalVars::g_hotelImageList.at(4).scaled(100,100)));
        case 4: ui->lb_image3->setPixmap(QPixmap::fromImage(GlobalVars::g_hotelImageList.at(3).scaled(100,100)));
        case 3: ui->lb_image2->setPixmap(QPixmap::fromImage(GlobalVars::g_hotelImageList.at(2).scaled(100,100)));
        case 2: ui->lb_image1->setPixmap(QPixmap::fromImage(GlobalVars::g_hotelImageList.at(1).scaled(100,100)));
        case 1: ui->lb_licenseImage->setPixmap(QPixmap::fromImage(GlobalVars::g_hotelImageList.at(0).scaled(100,100)));break;
    }
}

void RegisterHotel::setState(EditState state)
{
    m_state = state;
}

void RegisterHotel::forbidEveryOne()
{
    ui->le_hotelName->setEnabled(false);
    ui->le_hotelPhone->setEnabled(false);
    ui->le_hotelLocation->setEnabled(false);
    ui->sb_hotelHouseCount->setEnabled(false);

    ui->de_openedTime->setEnabled(false);
    ui->de_decoratedTime->setEnabled(false);
    ui->te_hotelCancel->setEnabled(false);
    ui->te_hotelDescription->setEnabled(false);
    ui->te_hotelIntroduce->setEnabled(false);

    ui->cb_highPrice->setEnabled(false);
    ui->cb_lowPrice->setEnabled(false);
    ui->cb_hotelState->setEnabled(false);
    ui->cb_hotelCity->setEnabled(false);
    ui->cb_hotelCounty->setEnabled(false);
    ui->cb_hotelStar->setEnabled(false);
    ui->cb_hotelType->setEnabled(false);

    ui->pb_uploadHotelPic->setEnabled(false);
    ui->pb_uploadLicense->setEnabled(false);
    ui->cb_freePark->setEnabled(false);
    ui->cb_payPark->setEnabled(false);
    ui->cb_transfer->setEnabled(false);
    ui->cb_bath->setEnabled(false);
    ui->cb_hairDryer->setEnabled(false);
    ui->cb_spa->setEnabled(false);
    ui->cb_wiredNet->setEnabled(false);
    ui->cb_wifiNet->setEnabled(false);
    ui->cb_baggage->setEnabled(false);
    ui->cb_restaurant->setEnabled(false);
}

void RegisterHotel::allowEveryOne()
{
    ui->le_hotelName->setEnabled(true);
    ui->le_hotelPhone->setEnabled(true);
    ui->le_hotelLocation->setEnabled(true);
    ui->sb_hotelHouseCount->setEnabled(true);

    ui->de_openedTime->setEnabled(true);
    ui->de_decoratedTime->setEnabled(true);
    ui->te_hotelCancel->setEnabled(true);
    ui->te_hotelDescription->setEnabled(true);
    ui->te_hotelIntroduce->setEnabled(true);

    ui->cb_highPrice->setEnabled(true);
    ui->cb_lowPrice->setEnabled(true);
    ui->cb_hotelState->setEnabled(true);
    ui->cb_hotelCity->setEnabled(true);
    ui->cb_hotelCounty->setEnabled(true);
    ui->cb_hotelStar->setEnabled(true);
    ui->cb_hotelType->setEnabled(true);

    ui->pb_uploadHotelPic->setEnabled(true);
    ui->pb_uploadLicense->setEnabled(true);
    ui->cb_freePark->setEnabled(true);
    ui->cb_payPark->setEnabled(true);
    ui->cb_transfer->setEnabled(true);
    ui->cb_bath->setEnabled(true);
    ui->cb_hairDryer->setEnabled(true);
    ui->cb_spa->setEnabled(true);
    ui->cb_wiredNet->setEnabled(true);
    ui->cb_wifiNet->setEnabled(true);
    ui->cb_baggage->setEnabled(true);
    ui->cb_restaurant->setEnabled(true);
}

void RegisterHotel::on_pb_finish_clicked()
{
    QString text = ui->pb_finish->text();
    if (text == "提交")
    {
        QString name = ui->le_hotelName->text();
        QString phone = ui->le_hotelPhone->text();
        QString location = ui->le_hotelLocation->text();
        int count = ui->sb_hotelHouseCount->value();
        QString description = ui->te_hotelDescription->document()->toPlainText();
        if (name.isEmpty() || phone.isEmpty() || location.isEmpty() || count == 0
                || description.isEmpty() || m_licenseImagePath.isEmpty() || m_imagePathList.isEmpty())
        {
            QMessageBox::warning(this, "警告", "请完善信息", QMessageBox::Yes, QMessageBox::Yes);
            return;
        }

        int lowPrice = ui->cb_lowPrice->currentText().toInt();
        int highPrice = ui->cb_highPrice->currentText().toInt();
        QString city = ui->cb_hotelState->currentText() +
                "-" + ui->cb_hotelCity->currentText() +
                "-" + ui->cb_hotelCounty->currentText();
        QString type = ui->cb_hotelType->currentText();
        QString star = ui->cb_hotelStar->currentText();
        QString openedTime = ui->de_openedTime->text();
        QString decoratedTime = ui->de_decoratedTime->text();
        QString introduce = ui->te_hotelIntroduce->placeholderText();
        QString cancel = ui->te_hotelCancel->document()->toPlainText();

        if (m_state == E_NewCreate)
        {
            QString id = QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz");
            QString user = GlobalVars::g_localUser.getNumber();
            HotelInfo hotel(id,user,name,location,star,type,city,phone,count,lowPrice,highPrice,
                            openedTime,decoratedTime,description,cancel,introduce,m_hotelServer.join(","),
                            m_licenseImagePath,m_imagePathList.join("#"));
            GlobalVars::g_localHotel = hotel;
            emit signalSubmitHotel();
        }
        else if (m_state == E_EditInfo)
        {
            if (m_hotel.getName() == name)
            {
                name = "";
            }
            if (m_hotel.getLocation() == location)
            {
                location = "";
            }
            if (m_hotel.getStar() == star)
            {
                star = "";
            }
            if (m_hotel.getType() == type)
            {
                type = "";
            }
            if (m_hotel.getCity() == city)
            {
                city = "";
            }
            if (m_hotel.getPhone() == phone)
            {
                phone = "";
            }
            if (m_hotel.getTotalHouse() == count)
            {
                count = 0;
            }
            if (m_hotel.getLowPrice() == lowPrice)
            {
                lowPrice = 0;
            }
            if (m_hotel.getHighPrice() == highPrice)
            {
                highPrice = 0;
            }
            if (m_hotel.getOpenedTime() == openedTime)
            {
                openedTime = "";
            }
            if (m_hotel.getDecoratedTime() == decoratedTime)
            {
                decoratedTime = "";
            }
            if (m_hotel.getDescription() == description)
            {
                description = "";
            }
            if (m_hotel.getCancellation() == cancel)
            {
                cancel = "";
            }
            if (m_hotel.getInstruction() == introduce)
            {
                introduce = "";
            }
            QString server = m_hotelServer.join(",");
            if (m_hotel.getServer() == m_hotelServer.join(","))
            {
                server = "";
            }

            if (m_licenseImagePath == " ")       /// 为了不修改证件照片时过提交的检测
            {
                m_licenseImagePath = "";
            }
            if (m_imagePathList.size() == 1)
            {
                if (m_imagePathList.at(0).isEmpty())
                {
                    m_imagePathList.clear();
                }
            }

            QString id = m_hotel.getHotel();
            QString user = m_hotel.getUser();
            HotelInfo hotel(id,user,name,location,star,type,city,phone,count,lowPrice,highPrice,
                            openedTime,decoratedTime,description,cancel,introduce,server,
                            m_licenseImagePath,m_imagePathList.join("#"));

            GlobalVars::g_localHotel = hotel;
            emit signalUpdateHotel();
        }

    }
    else if (text == "修改")
    {
        m_state = E_EditInfo;
        m_licenseImagePath = " ";       /// 为了不修改证件照片时过提交的检测
        m_imagePathList.append("");
        ui->pb_finish->setText("提交");
        ui->pb_cancel->setText("取消");
        allowEveryOne();
    }
    else if (text == "通过")
    {
        emit signalHotelPassCert(true, m_hotel.getHotel());
    }
    else{
        ;
    }
}
void RegisterHotel::on_pb_cancel_clicked()
{
    QString text = ui->pb_cancel->text();
    if (text == "退出")
    {
        this->close();
    }
    else if (text == "不通过")
    {
        emit signalHotelPassCert(false, m_hotel.getHotel());
    }
    else
    {
        ui->pb_cancel->setText("退出");
        ui->pb_finish->setText("修改");
        forbidEveryOne();
    }
}

///////////////////////////new create hotel////////////////////////////////
void RegisterHotel::constructeCondition(bool checked, const QString& text)
{
    if (checked)
    {
        m_hotelServer.append(text);
    }
    else
    {
        if (m_hotelServer.contains(text))
        {
            m_hotelServer.removeOne(text);
        }
    }
}
void RegisterHotel::on_cb_freePark_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_freePark->text());
}
void RegisterHotel::on_cb_bath_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_bath->text());
}
void RegisterHotel::on_cb_payPark_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_payPark->text());
}
void RegisterHotel::on_cb_hairDryer_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_hairDryer->text());
}
void RegisterHotel::on_cb_transfer_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_transfer->text());
}
void RegisterHotel::on_cb_spa_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_spa->text());
}
void RegisterHotel::on_cb_wiredNet_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_wiredNet->text());
}
void RegisterHotel::on_cb_wifiNet_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_wifiNet->text());
}
void RegisterHotel::on_cb_baggage_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_baggage->text());
}
void RegisterHotel::on_cb_restaurant_clicked(bool checked)
{
    constructeCondition(checked,ui->cb_restaurant->text());
}

void RegisterHotel::on_cb_lowPrice_currentIndexChanged(int index)
{
    if (m_state == E_NewCreate || m_state == E_EditInfo)
    {
        if (index > ui->cb_highPrice->currentIndex())
        {
            ui->cb_highPrice->setCurrentIndex(index);
        }
    }
}
void RegisterHotel::on_pb_uploadLicense_clicked()
{
    m_licenseImagePath = QFileDialog::getOpenFileName(this, tr("Open Image"),
                                                            "F:\\ForWork\\HotelPlatform\\image\\head_pic\\avatar",
                                                            tr("Image (*.png *.jpg)"));

    if (m_licenseImagePath.isEmpty())
    {
        return;
    }

    ui->lb_licenseImage->setPixmap(QPixmap::fromImage(QImage(m_licenseImagePath).scaled(100,100)));
}
void RegisterHotel::on_pb_uploadHotelPic_clicked()
{
    m_imagePathList = QFileDialog::getOpenFileNames(this, tr("Open Image"),
                                                            "F:\\ForWork\\HotelPlatform\\image\\head_pic\\avatar",
                                                            tr("Image (*.png *.jpg)"));

    if (m_imagePathList.size() == 0)
    {
        return;
    }

    ui->lb_image1->clear();
    ui->lb_image2->clear();
    ui->lb_image3->clear();
    ui->lb_image4->clear();
    ui->lb_image5->clear();
    ui->lb_image6->clear();
    switch (m_imagePathList.size())
    {
        case 6:
            ui->lb_image6->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(5)).scaled(100,100)));
        case 5:
            ui->lb_image5->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(4)).scaled(100,100)));
        case 4:
            ui->lb_image4->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(3)).scaled(100,100)));
        case 3:
            ui->lb_image3->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(2)).scaled(100,100)));
        case 2:
            ui->lb_image2->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(1)).scaled(100,100)));
        case 1:
        {
            ui->lb_image1->setPixmap(QPixmap::fromImage(QImage(m_imagePathList.at(0)).scaled(100,100)));
        } break;
        default:
        {
            ui->lb_image1->setText("至多上传6张图片");
        } break;
    }
}


///////////////////////////cert hotel////////////////////////////////
void RegisterHotel::closePushButton()
{
    ui->pb_uploadHotelPic->close();
    ui->pb_uploadLicense->close();
    ui->pb_finish->setText("通过");
    ui->pb_cancel->setText("不通过");
}
void RegisterHotel::errorShow(const QString& msg)
{
    QMessageBox::warning(this, "警告", msg, QMessageBox::Yes, QMessageBox::Yes);
}
